<?php
class stockModel extends RPHP_Model
{

    // 获取所有商品
    public function getAllGoods()
    {
        $sql = "SELECT `goods_id`,`name` FROM {$this->_prefix['primary']}agent_goods WHERE `status`=1 ORDER BY `goods_id` ASC";
        $tmp = $this->sql($sql);
        if ($tmp) {
            foreach ($tmp as $r) {
                $goods[$r['goods_id']] = stripslashes($r['name']);
            }

            return $goods;
        }

    }

    // 库存
    public function getStock($uid = 0, $goods_id = 0)
    {

        if ($uid == 0) {
            $uid = $this->_user['uid'];
        }

        if ($goods_id > 0) {
            $tsql = " AND `goods_id`=" . $goods_id;
        }

        $sql = "SELECT `goods_id`,`number` FROM {$this->_prefix['primary']}agent_stock WHERE `uid`=" . $uid . $tsql;
        $tmp = $this->sql($sql);
        if ($tmp) {

            foreach ($tmp as $r) {
                $data[$r['goods_id']] = $r['number'];
            }

            return $data;
        }
    }

    // 检查手机号码
    public function checkMobile($mobile)
    {
        $code = 404;

        $sql  = "SELECT `uid`,`name` FROM {$this->_prefix['primary']}member_account WHERE `pid`=" . $this->_user['uid'] . " AND `agent_level`>1 AND `mobile`='" . $mobile . "' AND `stat`=1 LIMIT 1";
        $data = $this->db->getrow($sql);
        if ($data) {
            $code = 200;
        }

        return array(
            'code' => $code,
            'data' => $data,
        );
    }

    // 出货库存
    public function transferStock($da)
    {
        $code = 900;

        $uid      = intval($da['uid']);
        $goods_id = intval($da['goods_id']);
        $number   = intval($da['number']);
        $mobile   = addslashes($da['mobile']);

        if ($uid > 0 && $goods_id > 0 && $number > 0 && strlen($mobile) == 11) {

            $user = $this->checkMobile($mobile);
            if ($user['code'] == 200 && $user['data']['uid'] == $uid) {

                $stock = $this->getStock(0, $goods_id);
                if ($stock[$goods_id] >= $number) {

                    // 开始转移库存

                    $this->db->begin();
                    $commit = false;
                    $code   = 403;

                    // 增加库存
                    $sql = "SELECT `id` FROM {$this->_prefix['primary']}agent_stock WHERE `uid`=" . $uid . " AND `goods_id`=" . $goods_id;
                    $id  = $this->db->getone($sql);

                    if ($id > 0) {
                        $sql = "UPDATE {$this->_prefix['primary']}agent_stock SET `number`=`number`+" . $number . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `id`=" . $id;
                    } else {
                        $sql = "INSERT INTO {$this->_prefix['primary']}agent_stock (`uid`,`goods_id`,`number`,`update_time`) VALUES (" . $uid . "," . $goods_id . "," . $number . "," . $_SERVER['REQUEST_TIME'] . ")";
                    }

                    $rcb = $this->sql($sql);
                    if ($rcb > 0) {
                        // 插入记录
                        $sql = "INSERT INTO {$this->_prefix['primary']}agent_stock_log (`goods_id`,`to_uid`,`from_uid`,`number`,`create_time`,`separate_type`) VALUES (" . $goods_id . "," . $uid . "," . $this->_user['uid'] . "," . $number . "," . $_SERVER['REQUEST_TIME'] . ",2)";

                        $rca = $this->sql($sql);
                        if ($rca > 0) {

                            // 减掉当前用户库存
                            $sql = "UPDATE {$this->_prefix['primary']}agent_stock SET `number`=`number`-" . $number . ",`update_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `uid`=" . $this->_user['uid'] . " AND `goods_id`=" . $goods_id;
                            $rcd = $this->sql($sql);

                            if ($rcd > 0) {
                                $commit = true;
                                $code   = 200;
                            }

                        }
                    }

                    if ($commit) {
                        $this->db->commit();
                    } else {
                        $this->db->rollback();
                    }
                } else {
                    $code = 502;
                }

            } else {
                $coed = 501;
            }
        }

        return array(
            'code' => $code,
        );
    }

    public function getSingleSetting($id)
    {
        // 获取设置信息
        $sql     = "SELECT `content` FROM {$this->_prefix['primary']}agent_setting WHERE `id`=" . $id;
        $content = $this->db->getone($sql);
        if ($content != "") {
            $json = json_decode($content, true);

            if ($json) {
                foreach ($json as $r) {
                    if ($r['number'] != "") {
                        $goods[$r['id']] = $r['number'];
                    }

                }

                return $goods;
            }
        }

    }

    public function getAgentSetting($da)
    {

        $code            = 404;
        $agent_level     = intval($da['agent_level']);
        $old_agent_level = intval($da['old_agent_level']);
        $uid             = intval($da['uid']);

        if ($agent_level > 0 && $uid > 0) {

            $sql = "SELECT `content`,`id` FROM {$this->_prefix['primary']}agent_setting WHERE `agent_level`=" . $agent_level . " AND `status`=1 ORDER BY `id` ASC";
            $tmp = $this->sql($sql);

            if ($tmp) {

                foreach ($tmp as $r) {
                    $data[$r['id']] = json_decode($r['content'], true);
                }

                $code = 200;

                if ($old_agent_level > 0) {
                    // 查找上一次升级的信息
                    $sql            = "SELECT `setting_id` FROM {$this->_prefix['primary']}agent_upgrade WHERE `agent_uid`=" . $uid . " AND `new_level`=" . $old_agent_level . " AND `stat`=1 ORDER BY `id` DESC LIMIT 1";
                    $old_setting_id = $this->db->getone($sql);
                    if ($old_setting_id > 0) {
                        $old_goods = $this->getSingleSetting($old_setting_id);

                        if ($old_goods) {

                            foreach ($data as $sid => $da) {

                                $unset = false;
                                foreach ($da as $key => $tm) {

                                    if ($tm['id'] != "") {
                                        if (!$old_goods[$tm['id']]) {
                                            $unset = true;
                                        } else {
                                            $data[$sid][$key]['number'] -= $old_goods[$tm['id']];
                                        }
                                    }

                                }

                                if ($unset) {
                                    unset($data[$sid]);
                                }
                            }

                        }
                    }
                }
            }
        }

        return array(
            'code'  => $code,
            'data'  => $data,
            'goods' => $this->getAllGoods(),
        );
    }
}
